package com.example.xwadmin.api.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import com.example.xwcommon.annotation.Login;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        if(handler instanceof HandlerMethod){
            HandlerMethod m=(HandlerMethod)handler;
            if(m.getMethodAnnotation(Login.class)==null){
                return true;
            }
        }
        if(request.getAttribute("cur_admin")==null){
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"code\":4001,\"msg\":\"请先登录\"}");
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
